package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"github.com/spf13/cast"
	"goravel/app/models"
	"strconv"
)

type UserController struct {
	//Dependent services
	SearchByParamsService
}

func NewUserController() *UserController {
	return &UserController{
		//Inject services

	}
}

func (r *UserController) Info(ctx http.Context) http.Response {
	var user models.User

	err := facades.Auth().User(ctx, &user) // 必须是指针
	facades.Orm().Query().Model(&models.User{}).Where("id = ?", user.ID).With("Configurations").With("Refillcards").First(&user)
	if err != nil {
		return ctx.Response().Json(http.StatusBadRequest, http.Json{
			"message": "用户不存在",
		})
	}
	return ctx.Response().Json(http.StatusOK, http.Json{
		"data": user,
	})
}

func (r *UserController) Index(ctx http.Context) http.Response {
	pageSize := ctx.Request().Query("pageSize", "1")
	pageSizeInt, _ := strconv.Atoi(pageSize)
	order := ctx.Request().Query("order")
	sort := ctx.Request().Query("sort")
	users := []models.User{}
	var total int64
	currentPage := ctx.Request().Query("currentPage", "1")
	currentPageInt, _ := strconv.Atoi(currentPage)
	queries := ctx.Request().Queries()
	if order != "" || sort != "" {
		facades.Orm().Query().Scopes(r.SearchByParams(ctx.Request().Queries())).With("Configurations").With("Refillcards").
			Order(sort+" "+order).Paginate(currentPageInt, pageSizeInt, &users, &total)
	} else {
		facades.Orm().Query().Scopes(r.SearchByParams(queries)).Order("id desc").With("Configurations").With("Refillcards").
			Paginate(currentPageInt, pageSizeInt, &users, &total)
	}

	return ctx.Response().Success().Json(http.Json{
		"data":  users,
		"total": total,
		"links": map[string]interface{}{
			"first": "http://" + ctx.Request().Host() + "/api/loginlog?pageSize=" + pageSize + "&currentPage=1",
			//最后一页应该是总数除以每页的数量取整
			"last": "http://" + ctx.Request().Host() + "/api/loginlog?pageSize=" + pageSize + "&currentPage=" + strconv.Itoa(int(total)/pageSizeInt),
			"prev": "http://" + ctx.Request().Host() + "/api/loginlog?pageSize=" + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt-1),
			"next": "http://" + ctx.Request().Host() + "/api/loginlog?pageSize=" + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt+1),
		},
		"meta": map[string]interface{}{
			"total_page":   int(total) / pageSizeInt,
			"current_page": currentPageInt,
			"per_page":     pageSizeInt,
			"total":        total,
		},
	})
}

// 删除用户
func (r *UserController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Input("id")
	var user models.User
	facades.Orm().Query().Where("id = ?", cast.ToInt(id)).First(&user)
	if user.ID == 0 {
		return ctx.Response().Status(http.StatusBadRequest).Json(http.Json{
			"message": "用户不存在",
		})
	}
	facades.Orm().Query().Delete(&user)
	return ctx.Response().Status(http.StatusOK).Json(http.Json{
		"message": "删除成功",
	})
}

// 显示用户
func (r *UserController) Show(ctx http.Context) http.Response {
	id := ctx.Request().Input("id")
	var user models.User
	facades.Orm().Query().Model(&models.User{}).Where("id = ?", id).With("Configurations").With("Refillcards").First(&user)
	if user.ID == 0 {
		return ctx.Response().Status(http.StatusBadRequest).Json(http.Json{
			"message": "用户不存在",
		})
	}
	return ctx.Response().Status(http.StatusOK).Json(http.Json{
		"message": "",
		"data":    user,
	})
}

// 修改密码
func (r *UserController) UpdatePassword(ctx http.Context) http.Response {
	type UserForm struct {
		ID       int    `json:"id"`
		Mobile   string `json:"mobile"`
		Password string `json:"password"`
	}
	var userForm UserForm
	err := ctx.Request().Bind(&userForm)
	if err != nil {
		return ctx.Response().Status(http.StatusBadRequest).Json(http.Json{
			"message": "参数错误",
		})
	}
	var user models.User
	facades.Orm().Query().Where("id = ?", userForm.ID).First(&user)
	if user.ID == 0 {
		return ctx.Response().Status(http.StatusBadRequest).Json(http.Json{
			"message": "用户不存在",
		})
	}
	user.Password, _ = facades.Hash().Make(userForm.Password)
	facades.Orm().Query().Update(&user)
	return ctx.Response().Status(http.StatusOK).Json(http.Json{
		"message": "修改成功",
	})

}
